{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<small> <i> This notebook was prepared by Marco Guajardo. For license visit [github](https://github.com/donnemartin/interactive-coding-challenges) </i> </small>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Solution notebook\n",
    "## Problem: Given a string of words, return a string with the words in reverse"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* [Constraints](#Constraints)\n",
    "* [Test Cases](#Test-Cases)\n",
    "* [Algorithm](#Algorithm)\n",
    "* [Code](#Code)\n",
    "* [Unit Test](#Unit-Test)\n",
    "* [Solution Notebook](#Solution-Notebook)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Constraints\n",
    "* Can we assume the string is ASCII?\n",
    "  * Yes\n",
    "* Is whitespace important?\n",
    "  * no the whitespace does not change\n",
    "* Is this case sensitive?\n",
    "  * yes\n",
    "* What if the string is empty?\n",
    "  * return None\n",
    "* Is the order of words important?\n",
    "  * yes\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Algorithm: Split words into a list and reverse each word individually\n",
    "Steps:\n",
    "\n",
    "* Check if string is empty\n",
    "* If not empty, split the string into a list of words \n",
    "* For each word on the list\n",
    "  * reverse the word\n",
    "* Return the string representation of the list\n",
    "\n",
    "Complexity:\n",
    "\n",
    "* Time complexity is O(n) where n is the number of chars.\n",
    "* Space complexity is O(n) where n is the number of chars. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reverse_words(S):\n",
    "    if len(S) is 0:\n",
    "        return None\n",
    "\n",
    "    words = S.split()\n",
    "    for i in range (len(words)):\n",
    "        words[i] = words[i][::-1]\n",
    "\n",
    "    return \" \".join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting reverse_words_solution.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile reverse_words_solution.py\n",
    "import unittest\n",
    "\n",
    "\n",
    "class TestReverseWords(unittest.TestCase):\n",
    "\n",
    "    def testReverseWords(self, func):\n",
    "            self.assertEqual(func('the sun is hot'), 'eht nus si toh')\n",
    "            self.assertEqual(func(''), None)\n",
    "            self.assertEqual(func('123 456 789'), '321 654 987')\n",
    "            self.assertEqual(func('magic'), 'cigam')\n",
    "            print('Success: reverse_words')\n",
    "\n",
    "\n",
    "def main():\n",
    "    test = TestReverseWords()\n",
    "    test.testReverseWords(reverse_words)\n",
    "\n",
    "\n",
    "if __name__==\"__main__\":\n",
    "  main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: reverse_words\n"
     ]
    }
   ],
   "source": [
    "run -i reverse_words_solution.py"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
